// endpoint/endpoint_reader.rs
use calamine::{open_workbook_auto, Reader, RangeDeserializerBuilder};
use serde::Deserialize;
use std::error::Error;

#[derive(Deserialize, Debug, Clone)]
pub struct Endpoint {
    pub method: String,   // 请求方法（GET、POST）
    pub path: String,     // 请求路径
}

impl Endpoint {
    // 从 Excel 文件中读取接口配置
    pub fn from_excel(file_path: &str) -> Result<Vec<Endpoint>, Box<dyn Error>> {
        let mut workbook = open_workbook_auto(file_path)?;
        let range = workbook.worksheet_range("Endpoints")
            .ok_or("Cannot find 'Endpoints' sheet in the Excel file")??;

        let mut iter = RangeDeserializerBuilder::new().from_range(&range)?;
        let mut endpoints = Vec::new();

        for result in iter {
            let record: Endpoint = result?;
            endpoints.push(record);
        }
        Ok(endpoints)
    }
}